import { Modal } from 'antd';
import TRNotification from '@/utils/notification';
import React from 'react';
import styles from './index.less';
import { ExclamationCircleOutlined } from '@/utils/antdIcons';

const msgConent = function (msg) {
  let arr = msg.split('*');
  if (arr.length === 1) {
    return msg;
  }
  return (
    <div>
      {arr.map((item, index) => (
        <div key={index}>{item}</div>
      ))}
    </div>
  );
};

const warningMsgConent = function (msg) {
  let arr = msg.split('*');
  if (arr.length === 1) {
    return msg;
  }
  return (
    <div className={styles.warningMsgConent}>
      {arr.map((item, index) => (
        <div key={index}>{item}</div>
      ))}
    </div>
  );
};

class AlertModal {
  __key__ = '';
  __modal__ = undefined;
  show = (msg) => {
    return new Promise((resolve) => {
      if (this.__key__ !== '') {
        return;
      }
      this.__key__ = String(Date.now());
      const that = this;
      Modal.confirm({
        title: msgConent(msg),
        centered: true,
        getContainer: false,
        okText: '确定',
        cancelText: '取消',
        icon: <ExclamationCircleOutlined />,
        onOk() {
          resolve({ index: 1 });
          that.__key__ = '';
        },
        onCancel() {
          resolve({ index: 0 });
          that.__key__ = '';
        },
      });
    });
  };
  warning = (msg) => {
    return new Promise((resolve) => {
      Modal.warning({
        centered: true,
        okText: '确认',
        getContainer: false,
        title: warningMsgConent(msg),
        onOk() {
          resolve({ index: 1 });
        },
      });
    });
  };
  Success = (msg) => {
    Modal.success({
      centered: true,
      okText: '确认',
      title: msgConent(msg),
    });
  };
  Info = (msg) => {
    Modal.info({
      centered: true,
      okText: '确认',
      title: msgConent(msg),
    });
  };
  longWarning = (msg, message) => {
    return new Promise((resolve) => {
      Modal.warning({
        centered: true,
        okText: '确认',
        title: msg,
        getContainer: false,
        content: msgConent(message),
        onOk() {
          resolve({ index: 1 });
        },
      });
    });
  };

  error = (msg, message) => {
    var obj = {};
    if (message) {
      obj.content = '失败原因：' + message;
    }
    Modal.error({
      centered: true,
      okText: '确认',
      title: msgConent(msg),
      ...obj,
    });
  };

  eamDelete = (title, content) => {
    return new Promise((resolve) => {
      if (this.__key__ !== '') {
        return;
      }
      this.__key__ = String(Date.now());
      const that = this;
      this.__modal__ = Modal.confirm({
        className: 'tabledelet',
        title: msgConent(title),
        content,
        centered: true,
        getContainer: false,
        okText: '确定',
        cancelText: '取消',
        icon: <ExclamationCircleOutlined />,
        onOk() {
          resolve({ index: 1 });
          that.__modal__ = undefined;
          that.__key__ = '';
        },
        onCancel() {
          resolve({ index: 0 });
          that.__key__ = '';
          that.__modal__ = undefined;
        },
      });
    });
  };
  eamUpdateDelete = (title, content) => {
    if (this.__modal__) {
      this.__modal__.update({
        title: msgConent(title),
        content,
      });
    }
  };
  dismiss = () => {
    if (this.__key__.length > 0) {
      TRNotification.remove(this.__key__);
      this.__key__ = '';
    }
    if (!this.__modal__) {
      TRNotification.remove(this.__modal__);
      this.__modal__ = undefined;
    }
  };
}

const TRAlert = new AlertModal();
export default TRAlert;
